package com.eliauk.democommon.controler;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.eliauk.democommon.common.Result;
import com.eliauk.democommon.query.PageParamDto;
import com.eliauk.democommon.query.PageQuery;
import com.eliauk.democommon.utils.app.ApprenticeUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;

/** 核心公共controller类
 */
public class BaseController<S extends IService<E>, E> {

    @Autowired
    protected S baseService;

    @ApiOperation("增")
    @PostMapping("/add")
    public Result insert(@RequestBody E entity) {
        baseService.save(entity);
        return Result.ok("添加成功");
    }

    @ApiOperation("删")
    @PostMapping("/deleteById")
    public Result delete(@RequestBody List<Integer> ids) {

        baseService.removeByIds(ids);
        return Result.ok("删除成功");
    }

    @ApiOperation("改")
    @PostMapping("/updateById")
    public Result updateById(@RequestBody E entity) {
        baseService.updateById(entity);
        return Result.ok("修改成功");
    }

    @ApiOperation("查")
    @GetMapping("/getById")
    public Result getById(@RequestParam Integer id) {
        return Result.ok(baseService.getById(id));
    }

    @ApiOperation("存")
    @PostMapping("/save")
    public Result<?> save(@RequestBody E entity) {
        baseService.saveOrUpdate(entity);
        return Result.ok("添加成功");
    }

    @ApiOperation("list查")
    @PostMapping("/list")
    public Result<List<E>> list(@RequestBody E entity) {
        QueryWrapper<E> queryWrapper = ApprenticeUtil.getQueryWrapper(entity);
        List<E> list = baseService.list(queryWrapper);
        return Result.ok(list);
    }

    @ApiOperation("page查")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageParamDto<E> pageParamDto) {
        //限制条件
        if (pageParamDto.getCurrPage() < 1) {
            pageParamDto.setCurrPage(1);
        }

        if (pageParamDto.getPageSize() > 100) {
            pageParamDto.setPageSize(100);
        }
        Page<E> page = new Page<>(pageParamDto.getCurrPage(), pageParamDto.getPageSize());
        QueryWrapper<E> queryWrapper = new QueryWrapper<>();
        //升序
        String asc = pageParamDto.getAsc();
        if (!StrUtil.isEmpty(asc) && !"null".equals(asc)) {
            String[] split = asc.split(",");
            queryWrapper.orderByAsc(Arrays.asList(split));
        }
        //降序
        String desc = pageParamDto.getDesc();
        if (!StrUtil.isEmpty(desc) && !"null".equals(desc)) {
            String[] split = desc.split(",");
            queryWrapper.orderByDesc(Arrays.asList(split));
        }
        Page<E> ePage = baseService.page(page, queryWrapper);
        return Result.ok(ePage);
    }

    @ApiOperation("获取数量")
    @PostMapping("/count")
    public Result count(@RequestBody E entity) {
        QueryWrapper<E> queryWrapper = ApprenticeUtil.getQueryWrapper(entity);
        long count = baseService.count(queryWrapper);
        return Result.ok(count);
    }
}